﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Controls
{
    public partial class TimKiemNangCao : System.Web.UI.UserControl
    {
        public static string  condition; 
        public static int all;
        public static string catCondition;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_TinhThanhPho");
                ListItem lt = new ListItem();
                lt = new ListItem("Chọn thành phố", "-1");
                ddlThanhPho.Items.Add(lt);
           
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["TinhThanhPho_Ten"].ToString(), dt.Rows[i]["TinhThanhPho_ID"].ToString());
                    ddlThanhPho.Items.Add(lt);
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_LoaiTinRao");
               
                lt = new ListItem("Chọn loại đăng tin", "-1");
                ddlLoai.Items.Add(lt);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["LoaiTinRao_TieuDe"].ToString(), dt.Rows[i]["LoaiTinRao_ID"].ToString());
                    ddlLoai.Items.Add(lt);
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_DonVi");

                lt = new ListItem("Chọn đơn vị", "-1");
                ddlDonvi.Items.Add(lt);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["DonVi_Name"].ToString(), dt.Rows[i]["DonVi_ID"].ToString());
                    ddlDonvi.Items.Add(lt);
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_DienTich");

                lt = new ListItem("Chọn diện tích", "-1");
                ddlDientich.Items.Add(lt);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["DienTich_From"].ToString() + " - " + dt.Rows[i]["DienTich_To"].ToString()+" m2", dt.Rows[i]["DienTich_ID"].ToString());
                    ddlDientich.Items.Add(lt);
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2","6");

                lt = new ListItem("Chọn danh mục", "-1");
                ddlDanhmuc.Items.Add(lt);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlDanhmuc.Items.Add(lt);
                }
            }
        }

        protected void ddlThanhPho_SelectedIndexChanged(object sender, EventArgs e)
        {
            int tttid = Convert.ToInt32(ddlThanhPho.SelectedValue);
           DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_QuanHuyen", "TinhThanhPho_ID", tttid.ToString());
            ListItem lt = new ListItem();
            ddlQuanHuyen.Items.Clear();
            lt = new ListItem("Chọn quận huyện", "-1");
            ddlQuanHuyen.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["QuanHuyen_Ten"].ToString(), dt.Rows[i]["QuanHuyen_ID"].ToString());
                ddlQuanHuyen.Items.Add(lt);
            }
        }

     

        protected void btn_search_Click(object sender, EventArgs e)
        {
            // Danh muc

            condition = "";

            catCondition = "";

            if (ddlDanhmuc.SelectedIndex > 0 && ddlDientich.SelectedIndex > 0 && ddlDonvi.SelectedIndex > 0 && ddlLoai.SelectedIndex > 0 && ddlQuanHuyen.SelectedIndex > 0 && ddlThanhPho.SelectedIndex > 0)
            {

                int id_danhmuc = Convert.ToInt32(ddlDanhmuc.SelectedValue);

                catCondition += " AND Category_ID1 = " + id_danhmuc;

                int id_thanhpho = Convert.ToInt32(ddlThanhPho.SelectedValue);

                condition += " TinhThanhPho_ID = " + id_thanhpho;

                int id_quanhuyen = Convert.ToInt32(ddlQuanHuyen.SelectedValue);

                condition += " AND QuanHuyen_ID = " + id_quanhuyen;

                int id_Dientich = Convert.ToInt32(ddlDientich.SelectedValue);

                DataTable temp = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_DienTich", "DienTich_ID", id_Dientich.ToString());

                condition += " AND SanGiaoDich_DienTich >= " + temp.Rows[0]["Dientich_From"].ToString() + " AND SanGiaoDich_DienTich <= " + temp.Rows[0]["Dientich_To"].ToString();

                int id_Donvi = Convert.ToInt32(ddlDonvi.SelectedValue);

                condition += " AND DonVi_ID = " + id_Donvi;

                condition += " AND SanGiaoDich_GiaBan >= " + txtFrom.Text + " AND SanGiaoDich_GiaBan <= " + txtTo.Text;

                int id_Loai = Convert.ToInt32(ddlLoai.SelectedValue);

                catCondition += " AND LoaiTinRao_ID = " + id_Loai;



                all = DAL.DCollector.DBCollector.Instance.CallStored("select * from tbl_SanGiaoDich where " + condition + catCondition, true).Rows.Count;
                Response.Redirect("/advance/" + LIB.Converter.convertLink(ddlDanhmuc.SelectedItem.Text) + "-" + id_Loai + "/search/reals");
            }
            else
            {
                Response.Redirect("/reals");
            }
           



        }
    }
}